package br.senai.mb;

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import br.senai.modelo.BaseDAO;

public abstract class ManagedBeanBase implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@SuppressWarnings({ "rawtypes" })
	protected BaseDAO createPersistence(Class serviceClass) {
		try {
			EntityManager entityManager = (EntityManager) getRequestAttribute("entityManager");
			Object persistence = (BaseDAO) serviceClass.newInstance();
			((BaseDAO) persistence).setEntityManager(entityManager);
			return (BaseDAO) persistence;
		} catch (Throwable e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	protected ExternalContext getExternalContext() {
		return getFacesContext().getExternalContext();
	}
	
	protected RequestContext getRequestContext() {
		return RequestContext.getCurrentInstance();
	}

	protected HttpServletRequest getRequest() {
		return (HttpServletRequest) getExternalContext().getRequest();
	}

	protected HttpServletResponse getResponse() {
		return (HttpServletResponse) getExternalContext().getResponse();
	}

	protected void sendRedirect(String url) throws IOException {
		getResponse().sendRedirect(url);
	}

	protected Locale getRequestLocale() {
		return getRequest().getLocale();
	}

	protected Flash getFlashScope() {
		return FacesContext.getCurrentInstance().getExternalContext().getFlash();
	}
	
	protected String getRequestParameter(String name) {
		return getExternalContext().getRequestParameterMap().get(name);
	}

	protected Object getRequestAttribute(String name) {
		return getExternalContext().getRequestMap().get(name);
	}

	protected void setRequestAttribute(String name, Object value) {
		getRequest().setAttribute(name, value);
	}
	
	protected HttpSession getSession() {
		return (HttpSession) getExternalContext().getSession(false);
	}

	protected void setSessionAttribute(String name, Object value) {
		getSession().setAttribute(name, value);
	}

	protected Object getSessionAttribute(String name) {
		return getSession().getAttribute(name);
	}

	protected void removeSessionAttribute(String nome) {
		getSession().removeAttribute(nome);
	}
	
	protected void addMessage(String clienteId, String msg) {
		FacesMessage message = new FacesMessage(msg);
		getFacesContext().addMessage(clienteId, message);
	}

	protected void addInfoMessage(String title, String message) {
		getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, title, message));
	}
	
	protected void addWarnMessage(String title, String message) {
		getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, title, message));
	}
	
	protected void addErrorMessage(String title, String message) {
		getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, title, message));
	}
	
	protected void addCallbackParam(String name, Object value) {
		getRequestContext().addCallbackParam(name, value);
	}
	
	protected void execute(String action) {
		getRequestContext().execute(action);
	}
}
